﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.ComponentModel;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using System.Web.UI.HtmlControls;

namespace GandA.Web.UI.Controls
{
    [
    AspNetHostingPermission(SecurityAction.Demand,
        Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand,
        Level = AspNetHostingPermissionLevel.Minimal),
    ]
    [ToolboxData("<{0}:Scripts runat=server></{0}:Scripts>")]
    [ToolboxBitmap(typeof(System.Web.UI.ScriptManager))]
    public class Scripts : WebControl
    {
        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Span; }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            bool linkIncluded = false;

            foreach (Control c in Page.Header.Controls)
            {
                if (c.ID == "StyleCombo2")
                {
                    linkIncluded = true;
                }
            }
            if (!linkIncluded)
            {
                HtmlGenericControl csslink = new HtmlGenericControl("link");
                csslink.ID = "StyleCombo2";
                csslink.Attributes.Add("href",
                                       Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.bootstrap-combined.min.css"));
                csslink.Attributes.Add("type", "text/css");
                csslink.Attributes.Add("rel", "stylesheet");
                Page.Header.Controls.Add(csslink);
            }

            string resourceName = "GandA.Web.UI.Controls.Scripts.jquery-1.9.1.js";

            ClientScriptManager cs;
            if (this.Page == null)
                cs = this.Parent.Page.ClientScript;
            else
                cs = this.Page.ClientScript;

            cs.RegisterClientScriptResource(typeof(GandA.Web.UI.Controls.DatePicker), resourceName);
            resourceName = "GandA.Web.UI.Controls.Scripts.Ctrls.js";
            cs.RegisterClientScriptResource(typeof(GandA.Web.UI.Controls.DatePicker), resourceName);
        }
    }
}
